package com.example.demo.controller;

import com.example.demo.VO.Result;
import com.example.demo.VO.WrongQuestionDetailedVO;
import com.example.demo.entity.*;
import com.example.demo.service.*;
import com.example.demo.webconfig.Annotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (WrongQuestion)表控制层
 *
 * @author hjl
 * @since 2020-12-01 11:48:18
 */
@RestController
@RequestMapping("wrongQuestion")
@Api(value = "错题集controller", tags = {"错题集操作接口"})
public class WrongQuestionController {
    /**
     * 服务对象
     */
    @Resource
    private WrongQuestionService wrongQuestionService;
    @Resource
    private ChoiceService choiceService;
    @Resource
    private JudgmentService judgmentService;
    @Resource
    private BlankService blankService;
    @Resource
    private Short1Service short1Service;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public WrongQuestion selectOne(Integer id) {
        return this.wrongQuestionService.queryById(id);
    }

    @ApiOperation(value = "错题集添加错题", tags = {"错题集添加错题"}, notes = "注意问题点")
    @GetMapping("addWrongQuestion")
    public Result addWrongQuestion(WrongQuestion wrongQuestion) {
        if (wrongQuestion.getUserId() == null || wrongQuestion.getUserId().equals("")) {
            return Result.failure("用户id不能为空");
        }
        if (wrongQuestion.getQuestionType() == null || wrongQuestion.getQuestionType().equals("")) {
            return Result.failure("题目类型type不能为空");
        }
        if (wrongQuestion.getQuestionId() == null || wrongQuestion.getQuestionId().equals("")) {
            return Result.failure("题目id不能为空");
        }
        if (wrongQuestion.getAnswer() == null || wrongQuestion.getAnswer().equals("")) {
            return Result.failure("题目错误答案不能为空");
        }
        wrongQuestionService.insert(wrongQuestion);
        return Result.success();
    }

    @ApiOperation(value = "更新错题集", tags = {"更新错题集"}, notes = "注意问题点")
    @GetMapping("updateWrongQuestion")
    public Result updateWrongQuestion(WrongQuestion wrongQuestion) {
        return Result.success(wrongQuestionService.update(wrongQuestion));
    }

    @ApiOperation(value = "通过题目id删除错题集里错题", tags = {"通过题目id删除错题集里错题"}, notes = "注意问题点")
    @GetMapping("deleteWrongQuestion")
    public Result deleteWrongQuestion(Integer questionId) {
        WrongQuestion wrongQuestion = wrongQuestionService.queryById(questionId);
        if (wrongQuestion.getQuestionId() == null || wrongQuestion.getQuestionId().equals("")) {
            return Result.failure("题目id不能为空");
        }
        return Result.success(wrongQuestionService.deleteById(questionId));
    }

    @Annotation.UserLoginToken
    @ApiOperation(value = "通过用户id获取错题集", tags = {"通过用户id获取错题集"}, notes = "注意问题点")
    @GetMapping("selectWrongQuestionByUserId")
    public Result selectWrongQuestionByUserId(Integer userId) {
        WrongQuestion wrongQuestion = new WrongQuestion();
        wrongQuestion.setUserId(userId);
        List<WrongQuestion> wrongQuestionList = wrongQuestionService.queryByExample(wrongQuestion);
        if (wrongQuestionList.size() == 0) {
            return Result.failure("没有错题集");
        }
        Map<Integer, Object> wrongQuestionMap = new HashMap<>();
        Integer id = 0;
        for (WrongQuestion wrongQuestion1 : wrongQuestionList) {
            String a = wrongQuestion1.getQuestionType();
            int b = Integer.parseInt(wrongQuestion1.getQuestionId());
            if (a.equals("1")) {
                Choice choice = choiceService.queryById(b);
                WrongQuestionDetailedVO wrongQuestionDetailedVO = new WrongQuestionDetailedVO(wrongQuestion1.getAnswer(), choice, a);
                wrongQuestionMap.put(id, wrongQuestionDetailedVO);
                id++;
            } else if (a.equals("2")) {
                Judgment judgment = judgmentService.queryById(b);
                WrongQuestionDetailedVO wrongQuestionDetailedVO = new WrongQuestionDetailedVO(wrongQuestion1.getAnswer(), judgment, a);
                wrongQuestionMap.put(id, wrongQuestionDetailedVO);
                id++;
            } else if (a.equals("3")) {
                Blank blank = blankService.queryById(b);
                WrongQuestionDetailedVO wrongQuestionDetailedVO = new WrongQuestionDetailedVO(wrongQuestion1.getAnswer(), blank, a);
                wrongQuestionMap.put(id, wrongQuestionDetailedVO);
                id++;
            } else if (a.equals("4")) {
                Short1 short1 = short1Service.queryById(b);
                WrongQuestionDetailedVO wrongQuestionDetailedVO = new WrongQuestionDetailedVO(wrongQuestion1.getAnswer(), short1, a);
                wrongQuestionMap.put(id, wrongQuestionDetailedVO);
                id++;
            }
        }
        return Result.success(wrongQuestionMap);
    }

}